package com.qlogic.commons.platform.impls.dal.hibernate.persistence;

import com.qlogic.commons.platform.ServiceExecutionException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * <pre>
 * Stock les information de la transaction:
 * <br> 
 * 	 - session Hibernate
 * <br>
 *   - transaction Hibernate
 * <br>
 *   - l'identifiant de la transaction
 * </pre>
 * @version $Date: 2005/08/01 15:28:30 $, $Revision: 1.1 $, branche-$Name:  $  
 * @author $Author: azi $ 
 */
public class HibernateSessionTransaction {

	/**
	 * Session Hibernate. 
	 */
	private Session session;
	/**
	 * Transaction Hibernate.
	 */
	private Transaction trx;
	/**
	 * L'identifiant de la transaction Hibernate.
	 */
	private long trxId;

	/**
	 * @param sessionFactory usine des sessions Hibernate
	 * @param trxId l'identifiant de la transaction
	 * @throws ServiceExecutionException
	 */
	HibernateSessionTransaction(SessionFactory sessionFactory, long trxId)
		throws ServiceExecutionException {
		this (sessionFactory, sessionFactory.openSession (), trxId);
	}

	/**
	 * @param sessionFactory usine des sessions Hibernate
	 * @param session session Hibernate
	 * @param trxId l'identifiant de la transaction
	 * @throws ServiceExecutionException
	 */
	/**
	 * @param sessionFactory
	 * @param session
	 */
	public HibernateSessionTransaction(SessionFactory sessionFactory, Object session, long trxId) throws ServiceExecutionException {
		if(session == null){
			throw new ServiceExecutionException ("Session non transmise");
		}
		try {
			this.session = (Session)session;
			trx = ((Session)session).beginTransaction();
			this.trxId = trxId;
		}catch(HibernateException e){
			throw new ServiceExecutionException(e);
		}
	}

	/**
	 * <pre>
	 * Renvoie la session Hibernate.
	 * </pre>
	 * @return renvoie la session Hibernate
	 */
	Session getSession () {
		return session;
	}

	/**
	 * <pre>
	 * Renvoie la transaction Hibernate.
	 * </pre>
	 * @return renvoie la transaction Hibernate
	 */
	Transaction getTrx() {
		return trx;
	}

	/**
	 * <pre>
	 * Renvoie l'identifiant de la transaction.
	 * </pre>
	 * @return renvoie l'identifiant de la transaction
	 */
	long getTrxId() {
		return trxId;
	}

	/**
	 * <pre>
	 * vide de la transaction.
	 * </pre>
	 */
	public void release() {
		trx = null;
		session = null;
	}

}
